<?php
/**
 * 供应商联系人管理
 */
namespace app\admin\controller;

use think\Request;
use app\admin\service\SupplierContact as SupplierContactService;
use app\admin\validate\SupplierContact as SupplierContactValidate;

class SupplierContact extends BaseController
{
    /**
     * 获取供应商列表
     * @param Request $request
     * @param SupplierContactService $purchaseContactService
     * @param SupplierContactValidate $validate
     * @return \think\response\Json
     */
    public function getList(Request $request,SupplierContactService $purchaseContactService,SupplierContactValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['supid'] =  $request->param('supid',null);
        $param['cname'] =  $request->param('cname',null);
        $param['phone'] =  $request->param('phone',null);
        $param['email'] =  $request->param('email',null);
        $param['wechat'] =  $request->param('wechat',null);

        $param['start_date'] =  $request->param('start_date',null);
        $param['end_date'] =  $request->param('end_date',null);

        $intPage =  $request->param('pageIndex',1);
        $intPageSize =  $request->param('pageSize',50);
        $arrData = $purchaseContactService->getList($param,(int)$intPage,(int)$intPageSize);
        return json($arrData);
    }

    /**
     * 供应商添加和修改
     * @param Request $request
     * @param SupplierContactService $purchaseContactService
     * @param SupplierContactValidate $validate
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(Request $request,SupplierContactService $purchaseContactService,SupplierContactValidate $validate){
        $checkData = $request->param();
        $checkType = isset($checkData['id']) ? 'save' : 'create';
//        if (!$validate->scene($checkType)->check($checkData)) {
//            return json(['code' => 1, 'message' => $validate->getError()]);
//        }

        $param['operator_id'] = $request->operator_id;
        $param['supid'] =  $request->param('supid',null);
        $param['cname'] =  $request->param('cname',null);
        $param['gender'] =  $request->param('gender',null);
        $param['birthday'] =  $request->param('birthday',null);
        $param['phone'] =  $request->param('phone',null);
        $param['email'] =  $request->param('email',null);
        $param['qq'] =  $request->param('qq',null);
        $param['wechat'] =  $request->param('wechat',null);
        $param['whatsapp'] =  $request->param('whatsapp',null);
        $param['dingtalk'] =  $request->param('dingtalk',null);
        $param['country'] =  $request->param('country',null);
        $param['address'] =  $request->param('address',null);
        $param['department'] =  $request->param('department',null);
        $param['position'] =  $request->param('position',null);
        $param['level'] =  $request->param('level',null);
        $param['tags'] =  $request->param('tags',null);
        $param['remark'] =  $request->param('remark',null);

        $param['id'] =  $request->param('id',null);
        $arrData = $purchaseContactService->save($param);
        return json($arrData);
    }

    /**
     * 供应商详情
     * @param Request $request 供应商ID
     * @return \think\response\Json 返回数据
     * @author huangfeng
     * @date 2024-6-28
     */
    public function getInfo(Request $request,SupplierContactService $purchaseContactService,SupplierContactValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('show')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['id'] = $request->param('id',null);
        $arrData = $purchaseContactService->getInfo($param);
        return json($arrData);
    }


    /**
     * 删除单条或多条数据
     * @param Request $request
     * @param SupplierContactValidate $validate
     * @param SupplierContactService $purchaseContactService
     * @return \think\response\Json
     */
    public function delete(Request $request,SupplierContactValidate $validate,SupplierContactService $purchaseContactService)
    {
        $checkData = $request->param();
        if (!$validate->scene('delete')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['ids'] = $request->param('ids',null);
        $arrData = $purchaseContactService->delete($param);
        return json($arrData);
    }

    public function getFieldEnum(Request $request,SupplierContactValidate $validate,SupplierContactService $purchaseContactService)
    {
        $params = $request->param();
        $arrData = $purchaseContactService->getFieldEnum($params['field'] ?? null);
        return json($arrData);
    }

    /**
     * 获取下拉框选项
     * @param Request $request
     * @param SupplierContactService $purchaseContactService
     * @param SupplierContactValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSelect(Request $request,SupplierContactService $purchaseContactService,SupplierContactValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $arrData = $purchaseContactService->getSelect();
        return json($arrData);
    }
}
?>
